<template>
  <!-- 修改密码的弹出看 -->
  <el-dialog title="密码修改" :visible.sync="centerDialogVisible" :append-to-body='true' :close-on-click-modal='false'
    :close-on-press-escape='false' :show-close='false' center>
    <el-form :model="ruleForm2" :rules="rules2" ref="ruleForm2" label-width="100px" class="demo-ruleForm">
      <el-form-item label="原密码" key="password" prop="password">
        <el-input v-model="ruleForm2.password" type="password" auto-complete="off" />
      </el-form-item>
      <el-form-item label="密码" key="newpassword1" prop="newpassword1">
        <el-input v-model="ruleForm2.newpassword1" type="password" auto-complete="off" />
      </el-form-item>
      <el-form-item label="确认密码" key="newpassword2" prop="newpassword2">
        <el-input v-model="ruleForm2.newpassword2" type="password" auto-complete="off" />
      </el-form-item>

    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitPwd('ruleForm2')">提交
      </el-button>
      <el-button @click="resetForm('ruleForm2')">重置</el-button>

    </span>
  </el-dialog>
</template>

<script>
  import request from '@/router/axios'
  import {
    mapState
  } from 'vuex'
  import {
    modifyCheck,
    updateUserPwd
  } from '@/api/admin/user'
  import {encryption} from '@/util/util'
  export default {
    computed: {
      ...mapState({
        userInfo: state => state.user.userInfo
      })
    },
    mounted(){
           // history.pushState(null, null, document.URL);
           // window.addEventListener('popstate', function () {
           //   history.pushState(null, null, document.URL);
           // });
   },
    data() {
      const validatePass = (rule, value, callback) => {
        if (this.ruleForm2.password !== '') {
          if (value !== this.ruleForm2.newpassword1) {
            callback(new Error('两次输入密码不一致!'))
          } else {
            callback()
          }
        } else {
          callback()
        }
      }
      return {
        centerDialogVisible: false,
        ruleForm2: {
          password: '',
          newpassword1: '',
          newpassword2: '',
          userId: ''
        },
        rules2: {
          password: [{
            required: true,
            min: 1,
            message: '原密码不能为空且不少于1位',
            trigger: 'blur'
          }],
          newpassword1: [{
            required: true,
            min: 1,
            max: 50,
            message: '新密码不能为空且长度在1到50个字符',
            trigger: 'blur'
          }, ],
          newpassword2: [{
              required: true,
              message: '确认密码不能为空',
              trigger: 'blur'
            },
            {
              validator: validatePass,
              trigger: 'blur'
            }
          ]
        }

      }
    },
    created() {
      this.checkPwd();
    },
    methods: {
      resetForm() {
        this.ruleForm2.password = undefined
        this.ruleForm2.newpassword1 = undefined
        this.ruleForm2.newpassword2 = undefined
        this.ruleForm2.userId = this.userInfo.id
      },
      submitPwd(formName) {
        this.$refs[formName].validate(valid => {
          if (!valid) {
            return false
          }
          const user = encryption({
            data: this.ruleForm2,
            key: 'scaxscaxscaxscax',
            param: ['password', 'newpassword1', 'newpassword2']
          })
          user.userId = this.userInfo.id;
          updateUserPwd(user).then(response => {
            this.$notify.success('修改成功')
            // 修改密码之后强制重新登录
              this.$store.dispatch('LogOut').then(() => {
                location.reload() // 为了重新实例化vue-router对象 避免bug
              })
          })
        })
      },
      checkPwd() {
        modifyCheck(this.userInfo.id).then((res) => {
          // debugger
          var result = res.data.data;
          if (result.tip) {
            //提示
            this.$notify({
              title: '警告',
              message: result.msg,
              duration: 0,
              type: 'warning'
            });
          }
          if (result.modify) {
            //修改
            this.centerDialogVisible = true;
          }

        })
      }
    }
  }
</script>

<style>
</style>
